//
//  LXDMVCollectionViewCell.m
//  BLDMusics
//
//  Created by lxd on 16/3/23.
//  Copyright © 2016年 李晓丹. All rights reserved.
//

#import "LXDMVCollectionViewCell.h"

@interface LXDMVCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
/**
 *  mv的数据
 */
@property (nonatomic, retain) NSMutableArray *mvArray;
/**
 *  上拉刷新的改变值
 */
@property (nonatomic, assign) NSInteger page;
/**
 *  用来接收第一层的tableView来改变滑动效果
 */
@property (nonatomic, retain)  UIScrollView *scroll;
/**
 *  用来记录MV的总数
 */
@property (nonatomic, assign) NSInteger count;

@end

@implementation LXDMVCollectionViewCell


-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createTableView];
    }
    return self;
}


- (void)setID:(NSNumber *)ID {
    _ID = ID;
    [self dataHandle];
    [self addFooter];
}


// 创建tableView
- (void)createTableView {
    _page = 1;
    self.mvArray = [NSMutableArray array];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.contentView addSubview:_tableView];
    [_tableView registerClass:[BYJMVTableViewCell class] forCellReuseIdentifier:@"mvCell"];
    _tableView.rowHeight = 100;

}

#pragma mark -  协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _mvArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BYJMVTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mvCell"];
    BYJMVModel *model = _mvArray[indexPath.row];
    cell.model = model;
    return cell;
}

// 点击MV
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    BYJMVModel *model = _mvArray[indexPath.row];
    
    BYJmvListModel *model1 = [[BYJmvListModel alloc] init];
    model1.url = model.url;
    model1.videoId = model.videoId;
    
    BYJMVViewController *vc = [[BYJMVViewController alloc] init];
    vc.model = model1;
    vc.videoId = model.videoId;
    [[self getViewController] pushViewController:vc animated:YES];
//    http://api.dongting.com/sim/mv/116322/similarity
}

// 获取当前navigation
- (UINavigationController*)getViewController {
    
    for (UIView* next = [self superview];next; next = next.superview) {
        
        UIResponder* nextResponder = [next nextResponder];
        
        if ([nextResponder isKindOfClass:[UINavigationController class]]) {
            
            return (UINavigationController *)nextResponder;
        }
        
    }
    return nil;
}


//http://api.dongting.com/song/singer/50530/videos?page=2
#pragma mark - MV数据解析
- (void)dataHandle {
    NSString *str = [NSString stringWithFormat:@"http://api.dongting.com/song/singer/%@/videos?page=%ld&app=ttpod&v=v8.1.5.2016022418&user_id=0&mid=iPhone7,2&f=f320&s=s310&imsi=&hid=&splus=9.2.1&active=1&net=2&openudid=921fb117b843753583dbf257222bb96a79d3c64e&idfa=5B1500CB-A041-4C95-951A-F6408B90A0BF&utdid=Vule3TyuSLwDAKd/fPxZrXTy&alf=201200&bundle_id=com.ttpod.music&latitude=38.8826342202686&longtitude=121.5392639454165",_ID, _page];
    NSLog(@"接口%@", str);
    [APPTools GETWithURL:str par:nil success:^(id responseObject) {
        self.count = [responseObject[@"totalCount"] integerValue];
        NSArray *tempArrr = [BYJMVModel modelHandleWithArray:responseObject[@"data"]];
        [self.mvArray addObjectsFromArray:tempArrr];
        // 如果mv 的总数==已经解析出来的数据， 就移除刷新
        if (_mvArray.count == _count) {
            [_tableView.mj_footer removeFromSuperview];
        }
        [_tableView reloadData];
        [_tableView.mj_footer endRefreshing];
    } filed:^(NSError *error) {
        
    }];
   
}
- (void)addFooter {
    // 上拉刷新-默认
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page ++;
        [self dataHandle];
    }];
    _tableView.mj_footer.automaticallyHidden = YES;
}






@end
